package com.example.myapplication.until;

import android.graphics.Point;
import android.view.MotionEvent;
import android.view.View;
import com.example.myapplication.view.MainView;
import com.example.myapplication.vo.Item;
import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.collision.Manifold;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.Contact;

import java.util.ArrayList;
import java.util.Iterator;


public class JBoxUtils {

    public static World world;
    public static int LAND_TYPE = 3;
    public static int BIRD_TYPE = 5;
    public static int PIG_TYPE = 6;
    public static ArrayList<Body> destoryList = new ArrayList<>() ;

    public static void init() {
        //重力方向向上与view方向相反
        world = new World(new Vec2(0, 10f));
        // 设置世界不会自动休眠
        world.setAllowSleep(true);

        world.setContactListener(new ContactListener() {
            @Override
            public void preSolve(Contact arg0, Manifold arg1) {
                //System.out.println("1");

                Object objA = arg0.m_fixtureA.getBody().m_userData;
                Object objB = arg0.m_fixtureB.getBody().m_userData;
                //System.out.println("1:  "+objA.getClass().getName()+"  与  "+objB.getClass().getName()+"发生碰撞");

            }

            @Override
            public void postSolve(Contact arg0, ContactImpulse arg1) {
                //System.out.println("2");
                Object objA = arg0.m_fixtureA.getBody().m_userData;
                Object objB = arg0.m_fixtureB.getBody().m_userData;
                //System.out.println(objA.getClass().getName()+"与"+objB.getClass().getName()+"发生碰撞");
                float[] a = arg1.normalImpulses ;
                float li = arg1.normalImpulses[0];
                if (objA instanceof Item) {
                    Item ia = (Item)objA ;
                    ia.hit(objB,li,arg1);
                }

                if (objB instanceof Item) {
                    if (objB instanceof Item) {
                        Item ib = (Item)objB ;
                        ib.hit(objA,li,arg1);
                    }
                }
            }

            @Override
            public void endContact(Contact arg0) {

            }

            @Override
            public void beginContact(Contact arg0) {
                //System.out.println("4");
            }
        });
    }

    public static Body createCircle(float r, float x, float y, int type,
                                    float angle, boolean isStatic){
        CircleShape shape = new CircleShape();
        shape.m_radius = r / Globals.RATE;

        Body body = createBody(shape, type, x, y, angle, isStatic);
        return body ;
    }

    public static Body createBox(float width, float height, float x, float y,
                                 float angle, int type, boolean isStatic) {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(width / 2 / Globals.RATE, height / 2 / Globals.RATE);

        return createBody(shape, type, x, y, angle, isStatic);
    }

    public static void  destory(Body body){
        destoryList.add(body) ;
    }

    public static void toDestory(){
        Iterator iterator = destoryList.iterator() ;
        while (iterator.hasNext()){
            Body body = (Body) iterator.next() ;
            int c = world.getBodyCount() ;
            JBoxUtils.world.destroyBody(body);
            if(c>world.getBodyCount()){
                iterator.remove();
            }
        }
    }

    private static Body createBody(Shape shape, int type, float x, float y,
                                   float angle, boolean isStatic) {
        FixtureDef def = new FixtureDef();
        def.shape = shape;

        switch (type) {
            case 1:
                def.density = 7.9f;             //密度
                def.restitution = 0.2f;         //恢复力
                def.friction = 0.1f;            //摩擦力
                break;
            case 2:
                def.density = 0.8f;
                def.restitution = 0.4f;
                def.friction = 0.2f;
                break;
            case 3:
                def.density = 0.3f;
                def.restitution = 0.1f;
                def.friction = 0.3f;
                break;
            case 4:
                def.density = 1.5f;
                def.restitution = 0.2f;
                def.friction = 0.4f;
                break;
            case 5:
            case 6:
                def.density = 0.2f;
                def.restitution = 0.2f;
                def.friction = 0.4f;
                break;
        }

        BodyDef bd = new BodyDef();
        // 旋转的弧度
        bd.angle = angle;
        bd.position = new Vec2(x / Globals.RATE, y / Globals.RATE);

        Body b = world.createBody(bd);
        if(b==null){
            return null ;
        }
        b.createFixture(def);
        if (isStatic) {
            b.setType(BodyType.STATIC);
        } else {
            b.setType(BodyType.DYNAMIC);
        }

        return b;
    }

}

